package com.loiot.baqi.service.job;


import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.loiot.baqi.constant.ApplicationConst;
import com.loiot.baqi.constant.Const;
import com.loiot.baqi.constant.DictionaryUtil;
import com.loiot.baqi.dao.ZpCompanyInfoDao;
import com.loiot.baqi.dao.ZpCompanyJobDemandKeysDao;
import com.loiot.baqi.dao.ZpCompanyJobInfoDao;
import com.loiot.baqi.dao.ZpPositionTypeKeysDao;
import com.loiot.baqi.pojo.ZpCompanyInfo;
import com.loiot.baqi.pojo.ZpCompanyJobDemandKeys;
import com.loiot.baqi.pojo.ZpCompanyJobInfo;
import com.loiot.baqi.pojo.ZpDictionaryInfo;
import com.loiot.baqi.pojo.ZpPositionTypeKeys;
import com.loiot.baqi.status.CompanyJobSourceType;
import com.loiot.baqi.status.CompanySourceType;
import com.loiot.baqi.status.DictionaryType;
import com.loiot.baqi.status.HDTypes;
import com.loiot.baqi.status.HeartbeatJobType;
import com.loiot.baqi.status.PauseStartType;
import com.loiot.baqi.utils.HdHttpUtils;
import com.loiot.baqi.utils.HtmlUnitUtil;
import com.loiot.baqi.utils.JsonToObject;
import com.loiot.baqi.utils.JsonUtils;
import com.loiot.baqi.utils.OSSUtils;
import com.loiot.baqi.utils.RegexpUtils;
import com.loiot.baqi.vo.hd.ComapnyAndComapnyJob;
import com.loiot.baqi.vo.hd.CoordInfo;
import com.loiot.baqi.vo.hd.Enterprise;
import com.loiot.baqi.vo.hd.Position;
import com.loiot.baqi.vo.hd.Root;
import com.loiot.commons.utils.FileUtil;
import com.loiot.commons.utils.JsonUtil;
import com.loiot.commons.utils.RandomUtil;
import com.loiot.commons.utils.StringUtil;



/**
 * 账号业务逻辑类
 * 
 * @author zhengrj
 * 
 */
@Service("fetchingHdCompanyAndJobInfoService")
@Scope("prototype")
public class FetchingHdCompanyAndJobInfoService extends JobTaskDefine {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
 	private ZpCompanyJobInfoDao zpCompanyJobInfoDao;
    @Resource
   	private ZpCompanyInfoDao zpCompanyInfoDao;
    
	@Resource
    ZpCompanyJobDemandKeysDao zpCompanyJobDemandKeysDao;
	@Resource
	ZpPositionTypeKeysDao zpPositionTypeKeysDao;
	
	HdHttpUtils httpUtils = new HdHttpUtils();
    
    public void doTask(){
    try {
    		httpUtils.setCookies("");
    		//log.info("ffffffffffffffffffffffffffffffffffffffffff");
			paseHdSiteInfo();
    	} catch (Exception e) {
			this.exceptionContent=e.getMessage();
		}finally{
			//添加心跳
			this.jobType=(int)HeartbeatJobType.FETCHING_HD_COMPANY_AND_JOB_INFO.getCode();
			this.addHeartbeat();
		}
}
	

    public  String getCoordInfoForHttp(String coordListJson) throws FailingHttpStatusCodeException, MalformedURLException, IOException{
		 //String  homeJson =FileUtil.readFileToString(new File("d:\\home_job.txt"));
    	/*if(StringUtil.isBlank(coordListJson)){
   		 	coordListJson =FileUtil.readFileToString(new File("d:\\coordListJson.txt"));
    	}else{
        	FileUtil.writeStringToFile("d:\\coordListJson.txt", coordListJson);
    	}*/
    	String url = ApplicationConst.ACCESS_DOMAIN+"getAreaCoordInfo.action";  
    	WebClient webClient = HtmlUnitUtil.WebClientInit();
		WebRequest req = new WebRequest(new URL(url), HttpMethod.POST);
		req.setCharset("utf-8");
		Map<String, String > params=new HashMap<String,String>();
		params.put("coordListJson", coordListJson);
		params.put("a", "中国");
		req.setRequestParameters(HtmlUnitUtil.translateParams(params));
        //final WebClient webClient = new WebClient();  
      
        // 1 启动JS  
        webClient.getOptions().setJavaScriptEnabled(true);  
        // 2 禁用Css，可避免自动二次请求CSS进行渲染  
        webClient.getOptions().setCssEnabled(false);  
        // 3 启动客户端重定向  
        webClient.getOptions().setRedirectEnabled(true);  
      
        // 4 js运行错误时，是否抛出异常  
        webClient.getOptions().setThrowExceptionOnScriptError(false);  
        // 5 设置超时  
        webClient.getOptions().setTimeout(50000);  
          
        HtmlPage htmlPage = webClient.getPage(req); 
        //webClient.addRequestHeader("coordListJson", coordListJson);

        // 等待JS驱动dom完成获得还原后的网页  
        webClient.waitForBackgroundJavaScript(10000);  
        // 网页内容  
       // System.out.println(htmlPage.asXml());  
        
        HtmlDivision div = htmlPage.getHtmlElementById("coordInfo");  
        String coordsJson = div.asText();
        webClient.closeAllWindows();
        log.info("coords info=" + coordsJson);
        return coordsJson;
    }
	
	
	

	
	//解析猎上网信息
	public  void paseHdSiteInfo() throws Exception{
		String userName =ApplicationConst.HD_HR_LOGIN_NAME;
		String pwd = ApplicationConst.HD_HR_LOGIN_PWD;
		//login(userName, pwd, "");
		String loginResult =httpUtils.login(userName, pwd, "");
		System.out.println("登陆成功，返回的个人信息="+loginResult);
		Map<String, Object> parseJSON2Map = JsonToObject.parseJSON2Map(loginResult);
		boolean flag =  (Boolean) parseJSON2Map.get("success");
		if(flag){
			String priority="priority=2&";
			priority="";
			
			//java
			String url4 = "http://hd.hunteron.com/api/v1/position/query?_t="+System.currentTimeMillis()+"&cityId=30101&"+priority+"query=Java&size=15&start=0";
			paseHdSiteInfo(url4,DictionaryUtil.getCode(DictionaryType.JOB_POSITION.getCode(), Const.ONE));
			
			//PHP
			url4 = "http://hd.hunteron.com/api/v1/position/query?_t="+System.currentTimeMillis()+"&cityId=30101&"+priority+"query=PHP&size=15&start=0";
			paseHdSiteInfo(url4,DictionaryUtil.getCode(DictionaryType.JOB_POSITION.getCode(), Const.TWO));		
			//Android
			url4 = "http://hd.hunteron.com/api/v1/position/query?_t="+System.currentTimeMillis()+"&cityId=30101&"+priority+"query=Android&size=15&start=0";
			paseHdSiteInfo(url4,DictionaryUtil.getCode(DictionaryType.JOB_POSITION.getCode(), Const.THREE));
	}
	}
	
	public void  paseHdSiteInfo(String positionURL,Long typeId) throws Exception{
		//String url4 = "http://hd.hunteron.com/api/v1/position/getHomepagePositions?_t="+System.currentTimeMillis()+"&size=2";
		String url4= positionURL;
		
		String homeJson = httpUtils.httpGet(url4,1000,2000); //获取企业信息
		
		  
		 //String  homeJson =FileUtil.readFileToString(new File("d:\\home_job.txt"));

		
		   HashMap map=null;
		   Root root=null;
		   List<CoordInfo> positons=new ArrayList<CoordInfo>();
		   List<ComapnyAndComapnyJob> ccs = new ArrayList<ComapnyAndComapnyJob>(); 
			     Root homeRoot = JsonUtils.fromJsonToObject(homeJson.trim(), Root.class);
			     int i=0;
			     for(Position p :homeRoot.getData().getPositions()){
			    	 //0：需要申请 1：不需要申请 2：不能申请-->
			    	 if(p.getPositionType()!=1){
			    		continue;
			    	 }
			    	 i++;
			    	 /*if(i==6){
			    		 break;
			    	 }*/
			    	 
			    	 
			    	 String url5="http://hd.hunteron.com/api/v1/position/detail?_t="+System.currentTimeMillis()+"&positionId="+p.getPositionId();
			 		 String jobRootJson = httpUtils.httpGet(url5,2000,4000); //获取职位信息
			 		 root = JsonUtils.fromJsonToObject(jobRootJson.trim(), Root.class);
			 		 
			 		 HashMap<String, Enterprise> enterMap = root.getData().getEnterprises();
			 		 Enterprise enterBean = enterMap.get(String.valueOf(p.getEnterpriseId()));
					 
			 		 System.out.println("start companyJob:"+p.getPositionId());
			 		 ZpCompanyJobInfo companyJob = convertCompanyJobInfoPoToLocalBean(root.getData().getPosition(), typeId,enterBean.getHighlights());
					 System.out.println(companyJob);
			 		 System.out.println("end companyJob:"+p.getPositionId());
			 		 
			 		 //System.out.println("start company:"+p.getEnterpriseId());
					 ZpCompanyInfo company = convertCompanyInfoPoToLocalBean(enterBean);
					 System.out.println(company);
			 		 //System.out.println("end company:"+p.getEnterpriseId());
			 		
			 		//坐标集合
			 		CoordInfo coord = new CoordInfo();
			 		coord.setHdLocation(companyJob.getAddress());
			 		coord.setHdPositionId(Integer.parseInt(companyJob.getHdCode().toString()));
			 		positons.add(coord);
			 		
			 		ComapnyAndComapnyJob cs = new ComapnyAndComapnyJob();
			 		cs.setCompany(company);
			 		cs.setCompanyJob(companyJob);
			 		//职位和企业信息
			 		ccs.add(cs);
			     }
			     
			     setCoordInfo(ccs, positons);
			     //FileUtil.writeStringToFile("d:\\companyAndJobAll.txt", JsonUtil.toJson(ccs));
		    	 this.persistenceInfoToDataBase(ccs);

		 
			String f="";
	}
	
	public void persistenceInfoToDataBase(List<ComapnyAndComapnyJob> ccs) throws Exception{
		Collections.reverse(ccs);//集合倒转
		for(ComapnyAndComapnyJob cj : ccs){
			HashMap<String, Object> pMap= new HashMap<String,Object>();
			pMap.put("hdCode", cj.getCompany().getHdCode());
			pMap.put("source",CompanySourceType.HD.getCode());
				List<ZpCompanyInfo> companyList = this.zpCompanyInfoDao.queryZpCompanyInfoList(pMap);
				ZpCompanyInfo company = cj.getCompany();
				company.setLastUpdateTime(new Date());
				company.setInPerson(1l);
				
				ZpCompanyJobInfo companyJob = cj.getCompanyJob();
				companyJob.setInPerson(1l);
				companyJob.setInDatetime(new Date());
				companyJob.setLastUpdateTime(new Date());
				if(companyList==null || companyList.size()==0){
					
					//设置企业logo
					String newhttpImageUrl=company.getLogoUrl().substring(company.getLogoUrl().lastIndexOf("?"));
					newhttpImageUrl=company.getLogoUrl().replace(newhttpImageUrl, "");
					String newFileName=RandomUtil.nextString(3)+newhttpImageUrl.substring(newhttpImageUrl.lastIndexOf("."));
			        String fileUrl = OSSUtils.uploadFileFromHttp("company/", company.getLogoUrl(),newFileName);
			        company.setLogoUrl(fileUrl);
			        
					this.zpCompanyInfoDao.addZpCompanyInfo(company);
					companyJob.setCompanyId(company.getCompanyId());
				}
				
				pMap.clear();
				pMap.put("hdCode", companyJob.getHdCode());
				pMap.put("source",CompanySourceType.HD.getCode());
				List<ZpCompanyJobInfo> companyJobList = this.zpCompanyJobInfoDao.queryZpCompanyJobInfoList(pMap);
				if(companyJobList==null || companyJobList.size()==0){
					if(companyJob.getCompanyId()==null){
						companyJob.setCompanyId(companyList.get(0).getCompanyId());
					}
					this.zpCompanyJobInfoDao.addZpCompanyJobInfo(companyJob);
					this.addCompanyJobKeys(companyJob);
				}
			
		}
	}
	
	/**
	 * 添加职位关键字
	 * @param job
	 * @throws Exception 
	 */
	public void addCompanyJobKeys(ZpCompanyJobInfo job) throws Exception{
		HashMap<String, Object> pMap = new HashMap<String, Object>();
		pMap.put("positionId", job.getTypeId());
		List<ZpPositionTypeKeys> keyList = zpPositionTypeKeysDao.queryZpPositionTypeKeysList(pMap);
		RegexpUtils regTools = new RegexpUtils();
		List<ZpPositionTypeKeys> exitsKeyList= new ArrayList<ZpPositionTypeKeys>();
		
		for(ZpPositionTypeKeys keyObj  : keyList){
			//如果存在，封装到新集合中
			if(regTools.isExitsKeyword(regTools.escapeKeyword(keyObj.getKeyword()), job.getMoreDesc())){
				exitsKeyList.add(keyObj);
			}
		}
		
		
		List <ZpCompanyJobDemandKeys>  demandKeys = new ArrayList<ZpCompanyJobDemandKeys>();
		for(ZpPositionTypeKeys exitsKeys : exitsKeyList){
			ZpCompanyJobDemandKeys key= new ZpCompanyJobDemandKeys();
			key.setInPerson(1l);
			key.setInTime(new Date());
			key.setJobId(job.getJobId());
			key.setKeyword(exitsKeys.getKeyword());
			key.setPositionId(job.getTypeId());
			demandKeys.add(key);
		}
		//批量更新职位关键字
		if(demandKeys.size()>0){
			pMap.clear();
			pMap.put("list", demandKeys);
			this.zpCompanyJobDemandKeysDao.batchSaveZpCompanyJobDemandKeys(pMap);
		}
		
		//标签匹配数量 更新
		for(ZpPositionTypeKeys exitsKeys : exitsKeyList){
			exitsKeys.setMatchCount(exitsKeys.getMatchCount()+1);
			this.zpPositionTypeKeysDao.updateZpPositionTypeKeys(exitsKeys);
		}
		
		//更新职位关键字数量
		if(demandKeys.size()>0){
			ZpCompanyJobInfo p = new ZpCompanyJobInfo();
			p.setJobId(job.getJobId());
			p.setKeywordStatus(demandKeys.size());
			System.out.println(p.getJobId() +":---------------:"+demandKeys.size());
			this.zpCompanyJobInfoDao.updateZpCompanyJobInfo(p);
		}
	}
	
	
	//设置坐标信息
	public  void setCoordInfo(List<ComapnyAndComapnyJob> ccs ,List<CoordInfo> positions) throws Exception{
		if(positions==null || positions.size()==0){
			return;
		}
		String coordListJson = JsonUtils.toJson(positions);
		coordListJson =getCoordInfoForHttp(coordListJson);
		List<CoordInfo> coords = JsonUtils.toList(coordListJson, CoordInfo.class);
		
		for(int i=0;i<coords.size();i++){
			CoordInfo coord = coords.get(i);
			ZpCompanyJobInfo job = ccs.get(i).getCompanyJob();
			job.setCoordX(coord.getCoordX());
			job.setCoordY(coord.getCoordY());
			String areaName =coord.getAreaName();
			if(!StringUtil.isBlank(areaName)){
				List<ZpDictionaryInfo> dicList = DictionaryUtil.getTypes(DictionaryType.COMPANY_AREA.getCode());
				for(ZpDictionaryInfo info :dicList){
					if(info.getShowName().equals(areaName)){
						job.setAreaId(info.getDictionaryId());
						break;
					}
				}
			}
			if(!StringUtil.isBlank(job.getCoordX()) && !StringUtil.isBlank(job.getCoordY()) && job.getAreaId()==null){
				log.warn("areaName=" +areaName +"  没有获取到行政区id ");
			}
		}
	}
	
	//转换公司信息
	public static ZpCompanyInfo convertCompanyInfoPoToLocalBean(Enterprise enterBean) throws OSSException, ClientException, MalformedURLException, IOException{
		ZpCompanyInfo company=new ZpCompanyInfo();
		company.setAddress(enterBean.getAddress());
		company.setName(enterBean.getName());
		company.setDesc("<div style=\"text-indent: 26px; white-space: pre-wrap; \">"+enterBean.getIntroduce()+"</div>");
		company.setScaleId(HDTypes.getScaleId(enterBean.getScale()));
		company.setRegtime(HDTypes.getRegTime(enterBean.getCreateTime()));
		company.setFinancingLevelId(HDTypes.getFinancingLevel(enterBean.getDevelopStatus()));
		company.setIndustryId(HDTypes.getIndustryId(enterBean.getIndustryIds()));
		company.setCompanyNature(HDTypes.getCompanyNature(enterBean.getStyle()));
		company.setIsDelete((int)PauseStartType.START.getCode());
		company.setHdCode(enterBean.getEnterpriseId());
		company.setSource((int)CompanySourceType.HD.getCode());
		company.setInTime(new Date());
		company.setLogoUrl(enterBean.getLogoUrl());
		return company;
	}
	
	//转换职位信息
	public static ZpCompanyJobInfo convertCompanyJobInfoPoToLocalBean(Position hdPosi,Long jobTypeId,String highlights){
		ZpCompanyJobInfo job =new ZpCompanyJobInfo();
		job.setTypeId(jobTypeId);
		job.setName(hdPosi.getPositionTitle());
		job.setJobPositionLevelId(HDTypes.getJobPositionLevelId(hdPosi.getPositionTitle(), hdPosi.getWorkExpRequired()));
		job.setReportObject(hdPosi.getReportTo());
		job.setZpPersonCount(hdPosi.getHeadCount());
		job.setExpectedYearMoneyStart(hdPosi.getMinShowAnnualSalary());
		job.setExpectedYearMoneyEnd(hdPosi.getMaxShowAnnualSalary());
		HDTypes.setWorkExpRequired(hdPosi.getWorkExpRequired(), job);
		job.setDownTeamPersonCount(HDTypes.getDownTeamPersonCount(hdPosi.getSubordinate()));
		job.setDesc("<span style=\"white-space: pre-wrap; \">"+hdPosi.getJobDescription()+"</span>");
		job.setMoreDesc("<span style=\"white-space: pre-wrap; \">"+hdPosi.getJobRequirement()+"</span>");
		job.setSex(HDTypes.getSexId(hdPosi.getGenderRequired()));
		job.setEducationId(HDTypes.getEducationId(hdPosi.getDegreeRequired()));
		job.setEnglishLevelId(HDTypes.getEnglishLevelId(hdPosi.getLanguageRequired()));
		
		job.setJobTemptText(highlights);
		job.setIsDelete((int)PauseStartType.START.getCode());
		job.setAddress(hdPosi.getLocation());
		job.setZpUrgencyStatusId(HDTypes.getZpUrgencyStatusId(hdPosi.getPriority()));
		job.setHdCode(Long.parseLong(String.valueOf(hdPosi.getPositionId())));
		job.setSource((int)CompanyJobSourceType.HD.getCode());
		job.setRequireInterviewNum(hdPosi.getInterviewTimes());
		return job;
	}
	
	
	


	
	
	
	
	

}
